package com.delivery.www.servlet;

import com.alibaba.fastjson.JSON;
import com.delivery.www.po.Coupon;
import com.delivery.www.po.HaveCoupon;
import com.delivery.www.po.User;
import com.delivery.www.service.CouponService;
import com.delivery.www.constant.Constants;
import com.delivery.www.service.HaveCouponService;
import com.delivery.www.service.ServiceImpl.CouponServiceImpl;
import com.delivery.www.service.ServiceImpl.HaveCouponServiceImpl;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 12970
 */
public class CouponServlet extends BaseServlet {
    CouponService couponService = new CouponServiceImpl();
    HaveCouponService haveCouponService = new HaveCouponServiceImpl();

    /**
     * 查询优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void selectCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        List<Coupon> coupons = couponService.getCoupon(storeId);
        writer.print(JSON.toJSONString(coupons));
    }

    /**
     * 删除优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void deleteCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int couponId = Integer.parseInt(req.getParameter("couponId"));
        boolean b = couponService.deleteCoupon(couponId);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 添加优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void addCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int storeId = Integer.parseInt(req.getParameter("storeId"));
        Float value = Float.valueOf(req.getParameter("value"));
        Float min = Float.valueOf(req.getParameter("min"));
        int validity = Integer.parseInt(req.getParameter("validity"));
        boolean b = couponService.addCoupon(storeId, value, min, validity);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 修改优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void modifyCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        int couponId = Integer.parseInt(req.getParameter("couponId"));
        String field = req.getParameter("field");
        String value = req.getParameter("value");
        boolean b = couponService.judgeField(couponId, field, value);
        if (b) {
            writer.print("ok");
        } else {
            writer.print("error");
        }
    }

    /**
     * 领取优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void receiveCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        if (user == null) {
            writer.print("login");
        } else {
            int couponId = Integer.parseInt(req.getParameter("couponId"));
            int storeId = Integer.parseInt(req.getParameter("storeId"));
            String s = haveCouponService.add(couponId, user.getUserId(), storeId, 0);
            writer.print(s);
        }
    }

    /**
     * 获取优惠券
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void getCoupon(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        if (user == null) {
            writer.print("login");
        } else {
            int storeId = Integer.parseInt(req.getParameter("storeId"));
            Float amount = Float.parseFloat(req.getParameter("amount"));
            List<HaveCoupon> userCoupon = haveCouponService.getUserCoupon(user.getUserId(), storeId, 0);
            List<Coupon> coupons = couponService.getUserCoupon(userCoupon, amount);
            writer.print(JSON.toJSONString(coupons));
        }
    }

    /**
     * 获取用户领取的优惠券id
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void userCoupon1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        List<HaveCoupon> userCoupon = haveCouponService.getCoupon(user.getUserId());
        List<Coupon> coupons = couponService.getAllCoupon();
        List<HaveCoupon> haveCoupons = haveCouponService.modifyStatusOver(coupons, userCoupon);
        writer.print(JSON.toJSONString(haveCoupons));
    }

    /**
     * 获取用户领取的优惠券的信息
     *
     * @param req  请求
     * @param resp 响应
     * @throws IOException 异常
     */
    public void userCoupon2(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter writer = resp.getWriter();
        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        List<HaveCoupon> userCoupon = haveCouponService.getCoupon(user.getUserId());
        List<Coupon> coupons = couponService.getAllCoupon();
        List<HaveCoupon> haveCoupons = haveCouponService.modifyStatusOver(coupons, userCoupon);
        List<Coupon> coupon = couponService.getUserCoupon(haveCoupons);
        writer.print(JSON.toJSONString(coupon));
    }
}
